﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;


namespace BaiMaSoft.SysManage
{
    /// <summary>
    /// UserManage 的摘要说明
    /// </summary>
    public class UserManage
    {
        protected BaiMaSoft.Conn.DataConn DataConn = new BaiMaSoft.Conn.DataConn();

        public SqlConnection myconn;
        public SqlCommand mycomm, mycomm3;
        public SqlDataReader mydr;
        public SqlDataAdapter myadapter;
        public DataSet myds;
        public DataView mydv;

        public UserManage()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        

        //获取角色表的相关信息
        public DataView GetMyUserInfo(string LogonID, string GroupID, string Effective, string SqlSort)
        {
            DataConn.Connection.Open();
            string mysql = " Sys_GetMyUserInfo '" + LogonID + "','" + GroupID + "','" + Effective + "','" + SqlSort + "' ";
            myadapter = new SqlDataAdapter(mysql, DataConn.Connection);
            myds = new DataSet();
            myadapter.Fill(myds, "a");
            mydv = new DataView();
            mydv = myds.Tables["a"].DefaultView;
            DataConn.Connection.Close();
            return mydv;
        }
        private int count = 0;
        private string getUser(string creator)
        {
            string returnValue = string.Empty;
            string temp = string.Empty;
            DataConn.Connection.Open();
            string mysql = " select LogonId from sys_user_info where creatrole='" + creator + "'";
            myadapter = new SqlDataAdapter(mysql, DataConn.Connection);
            myds = new DataSet();
            myadapter.Fill(myds, "a");
            mydv = new DataView();
            mydv = myds.Tables["a"].DefaultView;
            DataConn.Connection.Close();
            if (mydv.Count > 0)
            {
                ArrayList list = new ArrayList();
                for (int i = 0; i < mydv.Count; i++)
                {
                    returnValue += mydv[i][0].ToString() + "','";
                    list.Add(mydv[i][0].ToString());
                }
                ++count;
                HttpContext.Current.Session[count.ToString()] = list;
                list = (ArrayList)HttpContext.Current.Session[count.ToString()];
                for (int j = 0; j < list.Count; j++)
                {
                    temp = getUser(list[j].ToString());
                    if (temp != "")
                        returnValue += temp;

                }
                --count;
                return returnValue;
            }
            else
                return "";
        }
        //查询用户信息

        public DataView SearchUserInfo(string slogonID, string RoleCode, string CodeCol, string UserID, string SqlSort, string deleteFlag)
        {
            string create = HttpContext.Current.Session["Baimasoft_User_LogonID"].ToString();
            string temp = getUser(create);
            DataConn.Connection.Open();
            string stmp = "(1=1) ";
            if (slogonID != "")
            {
                stmp += " and  LogonID like '%" + slogonID + "%'";
            }

            if (RoleCode != "0")
            {
                stmp += " and Role_Code = '" + RoleCode + "'";
            }

            string mysql2 = "Sys_GetMyUserInfo '" + UserID + "','" + CodeCol + "','1','" + SqlSort + "'";
            mysql2 = @"select sys_user_info.ID
                    ,LogonID
                    ,Alias
                    ,Pers_ID
                    ,sys_Pers_info.Name PName
                    ,sys_user_info.Role_Code
                    ,Sys_Role_Info.Name RoleName
                    ,Sys_Menu_MyTree.Label First_Page_Name
                    ,sys_user_info.CreatRole
                    ,sys_user_info.Effective
                    ,sys_user_info.First_Page
                    ,sys_user_info.Groupid
                    ,Sys_Group_Info.Groupname
                    ,sys_user_info.DbRole_Code
                    ,Sys_DA_Role.Role_Name DbRole_Name
                     from sys_user_info
                    left join sys_Pers_info on sys_user_info.Pers_id = sys_Pers_info.ID
                    left join Sys_Role_Info on sys_user_info.Role_code = Sys_Role_Info.code
                    LEFT  JOIN Sys_Menu_MyTree on sys_user_info.First_Page = Sys_Menu_MyTree.ID
                    left join Sys_Group_Info on sys_user_info.groupid = Sys_Group_Info.groupid
                    left join Sys_DA_Role on sys_user_info.DbRole_Code= Sys_DA_Role.Role_Code where 1=1 ";

            if (temp.Length > 3)
                temp = temp.Substring(0, temp.Length - 3);
            mysql2 += " and sys_user_info.LogonID in('" + temp + "') and sys_user_info.creatrole in('" + create + "','" + temp + "')";
            //**************************************
            string[] codes = new string[] { };
            temp = string.Empty;
            if (CodeCol != "0")
            {
                if (CodeCol.IndexOf(",") != -1)
                {
                    codes = CodeCol.Split(',');
                    if (codes.Length > 1)
                        for (int i = 0; i < codes.Length; i++)
                        {
                            temp += codes[i].ToString() + "','";
                        }
                    if (temp.Length > 3)
                        temp = temp.Substring(0, temp.Length - 3);
                }
                else
                    temp = CodeCol;
                mysql2 += " and sys_user_info.groupid in('" + temp + "')";
            }
            //**************************************

            //if (UserID != "")
            //    mysql2 = "";
            if (deleteFlag != "")
                mysql2 += " and sys_user_info.Effective in ('" + deleteFlag + "')";
            else
                mysql2 += " and sys_user_info.Effective ='1'";
            //mysql2 += SqlSort;
            myadapter = new SqlDataAdapter(mysql2, DataConn.Connection);
            myds = new DataSet();
            myadapter.Fill(myds, "a");
            mydv = new DataView();
            mydv = myds.Tables["a"].DefaultView;
            mydv.RowFilter = stmp;
            DataConn.Connection.Close();
            return mydv;

            /* DataConn.Connection.Open();
             string mysql = "SELECT a.ID, a.LogonID, a.Alias,b.OrgID, a.Pers_ID, b.Name as PersName, a.Role_Code, c.Name AS Role_Name, a.First_Page, a.CreatRole, a.Effective FROM Sys_User_Info a LEFT OUTER JOIN Sys_Pers_Info b ON a.Pers_ID = b.ID LEFT OUTER JOIN Sys_Role_Info c ON a.Role_Code = c.Code where (1=1)";
             if (slogonID != "")
                 mysql += "and LogonID like '%'+('" + slogonID + "')+'%'";
             if (RoleName != "")
                 mysql += "and Role_Name like '%'+('" + RoleName + "')+'%'";
             mysql += " order by a.LogonID";
             myadapter = new SqlDataAdapter(mysql, DataConn.Connection);
             myds = new DataSet();
             myadapter.Fill(myds, "a");
             mydv = new DataView();
             mydv = myds.Tables["a"].DefaultView;
             DataConn.Connection.Close();
             return mydv;*/
        }


      
    }
}
